class Solution {//leetcode213
public:
    int rob_the_rest(vector<int>& nums,int start_pos,int end_pos){
        if(start_pos>end_pos){
            return 0;
        }
        //创建dp表
        int n=end_pos-start_pos+1;
        vector<int> f(n);
        vector<int> g(n);
        f[0]=nums[start_pos];
        g[0]=0;
        for(int i=1;i<n;i++){
            f[i]=nums[i+start_pos]+g[i-1];
            g[i]=max(f[i-1],g[i-1]);
        }
        return max(f[n-1],g[n-1]);
    }
    int rob(vector<int>& nums) {
        int n=nums.size();
        int x=nums[0]+rob_the_rest(nums,2,n-2);
        int y=rob_the_rest(nums,1,n-1);
        return max(x,y);
    }
};